package com.tjh4.exer;

import com.tjh2.util.JDBCUtils;

import java.util.Scanner;

/**
 * @ClassName Exer2Test
 * @Description ：
 * @Author tang'jia'hao
 * @Date 2021/9/16 19:20
 * @Version 1.0
 **/
public class Exer2Test {

    /**
     * 主测试程序
     */
    public static void main(String[] args) {
        // testInsert();
        // queryWithIDCardOrExamCard();
        testDeleteByExamCard();
    }

    /**
     * 插入学生信息
     */
    public static void testInsert() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("四级/六级:");
        int type = scanner.nextInt();
        System.out.print("身份证号:");
        String IDCard = scanner.next();
        System.out.print("准考证号:");
        String examCard = scanner.next();
        System.out.print("学生姓名:");
        String studentName = scanner.next();
        System.out.print("所在城市:");
        String location = scanner.next();
        System.out.print("学生成绩:");
        int grade = scanner.nextInt();

        String sql = "insert into examstudent(type,IDCard,examCard,studentName,location,grade)values(?,?,?,?,?,?)";
        int insertCount = JDBCUtils.update(sql, type, IDCard, examCard, studentName, location, grade);
        if(insertCount > 0){
            System.out.println("添加成功");
        }else{
            System.out.println("添加失败");
        }
    }

    /**
     * 查找学生信息
     */
    public static void queryWithIDCardOrExamCard(){
        System.out.println("请选择你要输入的类型:");
        System.out.println("a.准考证号");
        System.out.println("b.身份证号");
        Scanner scanner = new Scanner(System.in);
        String selection = scanner.next();
        if("a".equalsIgnoreCase(selection)){
            System.out.println("请输入准考证号:");
            String examCard = scanner.next();

            String sql = "select FlowID flowID,Type type,IDCard idCard,ExamCard examCard,StudentName studentName,Location location,Grade grade from examstudent where examCard = ?";
            Student student = JDBCUtils.getInstance(Student.class, sql, examCard);
            if(student != null){
                System.out.println("student = " + student);
            }else{
                System.out.println("输入的准考证号有误");
            }
        }else if(("b".equalsIgnoreCase(selection))){
            System.out.println("请输入身份证号:");
            String IDCard = scanner.next();

            String sql = "select FlowID flowID,Type type,IDCard idCard,ExamCard examCard,StudentName studentName,Location location,Grade grade from examstudent where IDCard = ?";
            Student student = JDBCUtils.getInstance(Student.class, sql, IDCard);
            if(student != null){
                System.out.println("student = " + student);
            }else{
                System.out.println("输入的身份证号有误");
            }
        }else{
            System.out.println("您的输入有误,请重新进入程序");
        }
    }

    /**
     * 根据考号删除指定学生的信息
     */
    public static void testDeleteByExamCard() {
        System.out.print("请输入学生的考号：");
        Scanner scanner = new Scanner(System.in);
        String examCard = scanner.next();

        String sql = "select FlowID flowID,Type type,IDCard idCard,ExamCard examCard,StudentName studentName,Location location,Grade grade from examstudent where examCard = ?";
        Student student = JDBCUtils.getInstance(Student.class, sql, examCard);
        if(student == null){
            System.out.println("查无此人，请重新输入");
        }else{
            String sql1 = "delete from examstudent where examCard = ?";
            int deleteCount = JDBCUtils.update(sql1, examCard);
            if(deleteCount > 0){
                System.out.println("删除成功");
            }else{
                System.out.println("删除失败");
            }
        }
    }
}
